package com.ruoyi.asset.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.asset.domain.*;
import com.ruoyi.asset.mapper.AstTaskMapper;
import com.ruoyi.asset.service.IAstDeviceRecordService;
import com.ruoyi.asset.service.IAstSiteService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.asset.mapper.AstTaskUserMapper;
import com.ruoyi.asset.service.IAstTaskUserService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 任务分配Service业务层处理
 * 
 * @author xunzhe
 * @date 2024-06-21
 */
@Service
public class AstTaskUserServiceImpl implements IAstTaskUserService 
{
    @Autowired
    private AstTaskUserMapper astTaskUserMapper;

    @Autowired
    private AstTaskMapper astTaskMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAstDeviceRecordService deviceRecordService;

    @Autowired
    private IAstSiteService siteService;

    /**
     * 查询任务分配
     * 
     * @param assignmentId 任务分配主键
     * @return 任务分配
     */
    @Override
    public AstTaskUser selectAstTaskUserByAssignmentId(Long assignmentId)
    {
        return astTaskUserMapper.selectAstTaskUserByAssignmentId(assignmentId);
    }

    /**
     * 查询任务分配列表
     * 
     * @param astTaskUser 任务分配
     * @return 任务分配
     */
    @Override
    public List<AstTaskUser> selectAstTaskUserList(AstTaskUser astTaskUser)
    {
        return astTaskUserMapper.selectAstTaskUserList(astTaskUser);
    }

    /**
     * 新增任务分配
     * 
     * @param astTaskUser 任务分配
     * @return 结果
     */
    @Override
    public int insertAstTaskUser(AstTaskUser astTaskUser)
    {
        astTaskUser.setCreateTime(DateUtils.getNowDate());
        return astTaskUserMapper.insertAstTaskUser(astTaskUser);
    }

    /**
     * 修改任务分配
     * 
     * @param astTaskUser 任务分配
     * @return 结果
     */
    @Override
    public int updateAstTaskUser(AstTaskUser astTaskUser)
    {
        return astTaskUserMapper.updateAstTaskUser(astTaskUser);
    }

    /**
     * 批量删除任务分配
     * 
     * @param assignmentIds 需要删除的任务分配主键
     * @return 结果
     */
    @Override
    public int deleteAstTaskUserByAssignmentIds(String assignmentIds)
    {
        return astTaskUserMapper.deleteAstTaskUserByAssignmentIds(Convert.toStrArray(assignmentIds));
    }

    /**
     * 删除任务分配信息
     * 
     * @param assignmentId 任务分配主键
     * @return 结果
     */
    @Override
    public int deleteAstTaskUserByAssignmentId(Long assignmentId)
    {
        return astTaskUserMapper.deleteAstTaskUserByAssignmentId(assignmentId);
    }

    public List<AstTaskUser> selectTaskUserList(SysUser user){
        return astTaskUserMapper.selectTaskUserList(user);
    }

    @Transactional
    public int assignment(Long taskId, String userIds){
        Long[] users = Convert.toLongArray(userIds);
        List<AstTaskUser> list = new ArrayList<>();
        AstTask astTask = astTaskMapper.selectAstTaskByTaskId(taskId);
        String[] siteIdStrs = {};
        if(astTask.getSiteIds()!=null && !astTask.getSiteIds().equals("")){
            siteIdStrs = astTask.getSiteIds().split(",");
        }

        for (Long userId : users){
            AstTaskUser taskUser = new AstTaskUser();
            taskUser.setTaskId(taskId);
            taskUser.setUserId(userId);
            taskUser.setCreateTime(new Date());
            taskUser.setCreateBy(ShiroUtils.getLoginName());
            list.add(taskUser);
            astTaskUserMapper.insertAstTaskUser(taskUser);
            for(String siteIdStr : siteIdStrs){
                AstTaskUserList taskUserList = new AstTaskUserList();
                taskUserList.setTaskId(taskId);
                taskUserList.setSiteId(Long.parseLong(siteIdStr));
                taskUserList.setUserId(userId);
                taskUserList.setAssignmentId(taskUser.getAssignmentId());
                astTaskUserMapper.insertAstTaskUserList(taskUserList);
            }
        }

        return 1;
    }

    @Transactional
    public int deleteUser(Long taskId, String userIds)
    {
        astTaskUserMapper.deleteUserList(taskId, Convert.toLongArray(userIds));
        return astTaskUserMapper.deleteUser(taskId, Convert.toLongArray(userIds));
    }

    public List<AstTaskUser> selectAstTaskByUserId(Long userId){
        return astTaskUserMapper.selectAstTaskByUserId(userId);
    }

    public List<AstTaskUser> selectTaskUserlistList(Long assignmentId){
        return astTaskUserMapper.selectTaskUserlistList(assignmentId);
    }

    @Transactional
    public int updateAstTaskUserList(AstTaskUserList taskUserList){
        taskUserList.setCompleteTime(new Date());
        astTaskUserMapper.updateAstTaskUserList(taskUserList);

        SysUser sysUser = userService.selectUserById(taskUserList.getUserId());
        String weixiuIdsStr = taskUserList.getDeviceWeixiuIds();
        String xiedaiIdsStr = taskUserList.getDeviceXiedaiIds();

        AstTaskUserList taskUserListData = astTaskUserMapper.selectTaskUserListByListId(taskUserList.getListId());
        AstSiteRecord siteRecord = new AstSiteRecord();
        siteRecord.setSiteId(taskUserListData.getSiteId());
        siteRecord.setCreateTime(new Date());
        siteRecord.setCreateBy(sysUser.getUserName());
        siteRecord.setUserId(sysUser.getUserId());
        if(weixiuIdsStr ==null || weixiuIdsStr.equals("")){
            if(xiedaiIdsStr ==null || xiedaiIdsStr.equals("")){
                siteRecord.setStatus("0");
            }
        }
        if(siteRecord.getStatus() == null){
            siteRecord.setStatus("1");
            siteRecord.setReason(""+taskUserList.getDeviceNoDescribe());
        }
        siteService.xunjian(siteRecord);

        if(weixiuIdsStr !=null && !weixiuIdsStr.equals("")){
            String[] weixiuIds = weixiuIdsStr.split(",");
            for(String idStr : weixiuIds){
                AstDeviceRecord deviceRecord = new  AstDeviceRecord();
                deviceRecord.setDeviceId(Long.parseLong(idStr));
                deviceRecord.setStatus("1");
                deviceRecord.setCreateBy(sysUser.getUserName());
                deviceRecord.setCreateTime(new Date());
                deviceRecord.setUserId(taskUserList.getUserId());
                deviceRecord.setReason("任务检测到需要维修_"+taskUserListData.getTaskId());
                deviceRecordService.insertAstDeviceRecord(deviceRecord);
            }
        }

        if(xiedaiIdsStr !=null && !xiedaiIdsStr.equals("")){
            String[] xiedaiIds = xiedaiIdsStr.split(",");
            for(String idStr : xiedaiIds){
                AstDeviceRecord deviceRecord = new  AstDeviceRecord();
                deviceRecord.setDeviceId(Long.parseLong(idStr));
                deviceRecord.setStatus("0");
                deviceRecord.setCreateBy(sysUser.getUserName());
                deviceRecord.setCreateTime(new Date());
                deviceRecord.setUserId(taskUserList.getUserId());
                deviceRecord.setReason("任务检测到异常需要携带_"+taskUserListData.getTaskId());
                deviceRecordService.insertAstDeviceRecord(deviceRecord);
            }
        }

        Long assignmentId = selectNoCount(taskUserList.getAssignmentId());
        if(assignmentId == null){
            AstTaskUser astTaskUser = new AstTaskUser();
            astTaskUser.setAssignmentId(taskUserList.getAssignmentId());
            astTaskUser.setStatus("1");
            updateAstTaskUser(astTaskUser);
        }

        return 1;
    }

    public Long selectNoCount(Long assignmentId){
        return astTaskUserMapper.selectNoCount(assignmentId);
    }


}
